<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>限流白名单</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <blockquote class="layui-elem-quote layui-text">
            限流白名单说明:1. <a href="#" target="_blank">限流白名单添加帮助文档</a>
            <a class="layui-btn" style="float:right" href="javascript:location.replace(location.href);" title="刷新">
              <i class="layui-icon">&#xe669;</i></a>
        </blockquote>
        <fieldset class="table-search-fieldset">
            <legend>规则添加</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <div class="layui-input-inline" style="width:150px"> 
                                <select name="ruletype" id="ruletype">
            	              			<option value="" selected="">规则类型</option>
            	              			<option value="ip">IP</option>
            	              			<option value="url">URL</option>
            	              			<option value="useragent">UserAgent</option>
            	              			<option value="args">Args</option>
            	              			<option value="referer">Referer</option>
                		            </select>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <div class="layui-input-inline" style="width:250px">
                                <input type="text" name="ruleitem" autocomplete="off" placeholder="添加规则" lay-verify="required" lay-reqtext="不能为空" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <div class="layui-input-inline" style="width:200px">
                                <input type="text" name="remarks" autocomplete="off" placeholder="备注" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn"  lay-submit lay-filter="Add"><i class="layui-icon">&#xe61f;</i> 添加</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-table" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <!-- <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button> -->
            </div>
        </script>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script type="text/html" id="EnableTpl">
  <input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch" lay-text="启用|停用" lay-filter="wafRuleEnable" {{ d.enable == true ? 'checked' : '' }}>
</script>
<script>
    function getQueryString(name) {
      let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
      let r = window.location.search.substr(1).match(reg);
      if (r != null) {
          return unescape(r[2]);
      };
      return null;
    };
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: '/xwaf/reqlimit/white/List' + location.search,
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter'],
            cols: [[
                //{type: "checkbox", width: 50},
                {field: 'id',width:60,  title: 'ID', sort: true},
                {field: 'appname', title: '应用名', hide:true},
                {field: 'ruletype', title: '规则类型', sort: true},
                {field: 'ruleitem',width:'40%',edit:'text', title: '规则', sort: true},
                {field: 'enable',title:'状态',sort: true,templet: '#EnableTpl'},  
                {field: 'remarks',title: '备注',edit:'text'},
                {field: 'createtime', title: '创建时间', hide:true},
                {field: 'updatetime', title: '更新时间', hide:true},
                {title: '操作', minWidth:120, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [25, 50, 100],
            limit: 15,
            page: true
        });

      //table里状态
      form.on('switch(wafRuleEnable)', function(data){
        var bodystr = {"name":this.name,"value":this.checked};
    			  $.ajax({
    			   url: "/xwaf/reqlimit/white/Edit" + location.search+"&id="+this.value,
    			   headers:{'X-Requested-With':'XMLHttpRequest'},
             type: "POST",
             data: JSON.stringify(bodystr),
             dataType: "json",
    			   success: function (resp) {
    					  if (resp.code == 0 ) {
                  layer.msg('更改成功', {icon: 1});
                }else{
                  layer.msg(resp.msg,{icon:2});
                }
    				  }
    		    });
      });

  	//监听单元格编辑
  	table.on('edit(currentTableFilter)', function(obj){
    	var value = obj.value //得到修改后的值
    	,datas = obj.data //得到所在行所有键值
    	,field = obj.field; //得到字段
      if(datas.appname == "sys") {
        layer.msg('系统规则不能修改',{icon:2});
        return
      }
      var bodystr = {"id":datas.id,"name":field,"value":value};
        layer.confirm('确定修改?', {icon: 3, title:'提示',yes: function(index){ 
         	$.ajax({
             	//url: "/xwaf/rule/v1/Edit?id="+datas.id,
             	url: "/xwaf/reqlimit/white/Edit" + location.search+"&id="+datas.id,
              type: "POST",
              data: JSON.stringify(bodystr),
              dataType: "json",
             	success: function (resp) {
                 	if(resp.code == 0){
                    	layer.msg('['+ datas.id +'] ' + field + '更改为:'+ value);
                 	}else{
                    	layer.msg('['+ datas.id +'] ' + field + '更改失败',{icon:2});
                 	}
            	}
        	});	
        },
        cancel: function(index, layero){ 
              layer.close(index);
        } 
        });	
  	});
    //添加
    form.on('submit(Add)', function(datas){		
        datas.field.ruleitem = datas.field.ruleitem.replace(/^\s*|\s*$/g,""); //替换空格
		    console.log(JSON.stringify(datas.field)); 
        layer.confirm('确定添加?', {icon: 3, title:'提示',yes: function(index){
                     $.ajax({
                       url: "/xwaf/reqlimit/white/Add"+location.search,
                       type: "POST",
                       data: JSON.stringify(datas.field),
                       dataType: "json",
                       success: function (resp) {
					                console.log(resp);
                           if(resp.code == 0){
                               layer.msg('添加成功', {
                                          icon: 1,
                                          time: 2000
                                        },function(){
                                            window.location.reload();
                                });  
                           }else{
                               layer.msg('添加失败',{icon:2});
                           }
                        }
                     });		  	  
               },
                cancel: function(index, layero){ 
                    layer.close(index);
                } 
        }); 	                       	   
	      return false; 	
		});	

        //监听表格复选框选择
        //table.on('checkbox(currentTableFilter)', function (obj) {
         //   console.log(obj)
        //});

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['55%', '92%'],
                    content: '/page/cachetools/edit.html?id='+data.Id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                //console.log(data);
                layer.confirm('确定删除'+data.id+'?', {icon: 3, title:'删除提示',yes: function(index){
                     var index2 = layer.load(0, {shade: false});
                     layer.msg('请稍等...');
                			 $.ajax({
                			    url: "/xwaf/reqlimit/white/Del?id="+data.id,
                			    type: "get",
                			    success: function (resp) {
                            layer.close(index2);

                						 if(resp.code == 0){
                							 layer.msg('删除成功', {icon: 1});
                							 //window.location.reload();
                							 obj.del();
                						 }else{
                							 layer.msg(resp.msg,{icon:2});
                						 }
                				  }
                		   });		  	  
                },
            		  cancel: function(index, layero){ 
            			    layer.close(index);
                      layer.close(index2);
            			    console.log("不操作");
            		  } 
        		    });                
            }
        });
    });
</script>

</body>
</html>